//
//  MineViewController.m
//  BaseAPP
//
//  Created by Evisu on 16/12/16.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "MineViewController.h"
#import "MineViewCell.h"
#import "MineUserInfoCell.h"
#import "SettingViewController.h"

@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate>{
    
    NSArray *_titleNames;
    NSArray *_imageNames;
}

@property (nonatomic, strong)UITableView *tableView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _titleNames = @[@[@"个人信息"], @[@"相册", @"收藏", @"钱包", @"卡包"],@[@"表情"],@[@"设置"]];
    
    _imageNames = @[@[@"userInfo"], @[@"MoreMyAlbum", @"MoreMyFavorites", @"MoreMyBankCard", @"MyCardPackageIcon"],@[@"MoreExpressionShops"],@[@"MoreSetting"]];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.sd_layout
    .topSpaceToView(self.view, KSTATUS_AND_NAVIGATION_HEIGHT)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomSpaceToView(self.view, KTABBAR_HEIGHT);
    
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] init];
        _tableView.backgroundColor = KBACKGROUND_COLOR;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width_sd, 20)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    
    return _tableView;
}

#pragma mark - tableView dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _titleNames.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *titleOfSection = _titleNames[section];
    
    return titleOfSection.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reusedId = @"discover";
    static NSString *reusedId_userInfo = @"discover_userInfo";
    
    if (indexPath.section == 0) {
        //用户信息
        MineUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId_userInfo];
        
        if (!cell) {
            
            cell = [[MineUserInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId_userInfo];
        }
        
        return cell;
        
    } else {
    
        //相册、收藏、钱包等列表项
        MineViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reusedId];
        
        if (!cell) {
            
            cell = [[MineViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId];
        }
        
        NSArray *titles = _titleNames[indexPath.section];
        NSString *titleText = titles[indexPath.row];
        
        NSArray *images = _imageNames[indexPath.section];
        NSString *imageStr = images[indexPath.row];
        
        cell.labTitle.text = titleText;
        UIImage *img = [UIImage imageNamed:imageStr];
        img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        cell.ivIcon.image = img;
        return cell;
    }
  
    return 0;
    
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 88.0;
    }
    
    return 44.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    static NSString *headerReusedId = @"header";
    
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReusedId];
    
    if (!headerView) {
        
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerReusedId];
        
    }
    
    headerView.contentView.backgroundColor = KBACKGROUND_COLOR;
    
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.selected = NO;
    
    DMLog(@"did clicked");
    
    NSArray *titles = _titleNames[indexPath.section];
    NSString *titleText = titles[indexPath.row];
    
    SettingViewController *VC = [[SettingViewController alloc] init];
    VC.title = titleText;
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

//取消tableView-headerView的粘性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 20;
        
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
    }
    
}


@end
